package cn.Mmovie.dao.Impl;
import cn.Mmovie.bean.Administrator;
import cn.Mmovie.dao.AdministratorDao;
import cn.Mmovie.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AdministratorDaoImpl implements AdministratorDao {
    private static Connection conn= DbUtil.getConnection();
    private static PreparedStatement psmt=null;

    public static boolean isValidate(String username,String password){
        boolean isValidate=false;
        try {
            String sql = "SELECT username,password FROM tbl_admin WHERE username=? AND password=?";
            psmt = conn.prepareStatement(sql);
            psmt.setString(1, username);
            psmt.setString(2, password);
            Integer result = psmt.executeUpdate();
            if (result > 0) {
                isValidate = true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return isValidate;
    }
    public static void updatePassword(String username,String newPassword){
        try {
            String sql="UPDATE tbl_admin SET password=? WHERE username=?";
            psmt=conn.prepareStatement(sql);
            psmt.setString(1,newPassword);
            psmt.setString(2,username);
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    public Administrator selectUserByUsernameAndPassword(String username, String password) {
        Administrator admin = new Administrator();
        try {
            Connection conn = DbUtil.getConnection();
            String sql = "SELECT username,password FROM tbl_admin WHERE username=? AND password=?";
            PreparedStatement psmt = conn.prepareStatement(sql);
            psmt.setString(1, username);
            psmt.setString(2, password);
            ResultSet rs = psmt.executeQuery();
            if (rs.next()) {
                admin.setUsername(rs.getString("username"));
                admin.setPassword(rs.getString("password"));
            }
            DbUtil.close(rs, psmt, conn);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return admin;
    }

    @Override
    public Integer insert(Administrator administrator) {
        return null;
    }

    @Override
    public Integer delete(Integer id) {
        return null;
    }

    @Override
    public Integer update(Administrator administrator) {
        return null;
    }

    @Override
    public Administrator selectById(Integer id) {
        return null;
    }

    @Override
    public List<Administrator> selectAll() {
        return null;
    }
}
